package com.jingdong.dao.product.impl;

import com.jingdong.dao.product.ProductDao;
import com.jingdong.model.Product;
import com.jingdong.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> productList(Map<String,String> map) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            List<Product> productList=sqlSession.selectList("com.jingdong.productModel.list",map);
            sqlSession.close();
            return productList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Product getById(int id) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            Product product=sqlSession.selectOne("com.jingdong.productModel.getById",id);
            sqlSession.close();
            return product;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Product product) {
        try{
          SqlSession sqlSession=MyBatisUtils.getSqlSession();
          int updateResult=sqlSession.update("com.jingdong.productModel.update",product);
          sqlSession.commit();
          sqlSession.close();
          return updateResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int id) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
            int deleteResult=sqlSession.delete("com.jingdong.productModel.delete",id);
            sqlSession.commit();
            sqlSession.close();
            return deleteResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
